package com.jsz.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jsz.constant.MessageConstant;
import com.jsz.constant.RedisMessageConstant;
import com.jsz.entity.PageResult;
import com.jsz.entity.QueryPageBean;
import com.jsz.entity.Result;
import com.jsz.pojo.Member;
import com.jsz.service.MemberService;
import com.jsz.service.OrderService;
import com.jsz.service.OrderSettingListService;
import com.jsz.utils.DateUtils;
import com.jsz.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * 预约列表管理
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/orderSettinglist")
public class OrderSettingListcontroller {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    @Reference
    private OrderSettingListService orderSettingListService;

    @Reference
    private OrderService orderService;

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderSettingListService.findByPage(queryPageBean);
        return pageResult;
    }

    //添加数据
    @RequestMapping("/add/{setmealIds}")
    @PreAuthorize("hasAuthority('ADD_ORDERSETTING')")//添加预约列表数据权限
    public Result add(@RequestBody Map map, @PathVariable Integer[] setmealIds) {
        try {
            orderSettingListService.add(map, setmealIds);
            //添加数据成功
            return new Result(true, MessageConstant.ADD_ORDERSETTINGLIST_SUCCESS);
        } catch (RuntimeException r) {
            //阻止用户选择多个套餐
            return new Result(false, r.getMessage());
        } catch (Exception e) {
            //添加数据失败
            return new Result(false, MessageConstant.ADD_ORDERSETTINGLIST_FAIL);
        }
    }

    //预约成功,给用户发送短信
    @RequestMapping("/send4OrderSettingList")
    @PreAuthorize("hasAuthority('SEND_SUCCESSMESSAGE_ORDERSETTING')")//发送预约成功信息权限
    public Result send4OrderSettingList(@RequestBody Map map) throws Exception {
        String phoneNumber = (String) map.get("phoneNumber");
        String Date = (String) map.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(Date);
        try {
            //使用阿里云短信服务工具类发送验证码
            SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, phoneNumber, DateUtils.parseDate2String(orderDate));
        } catch (Exception e) {
            e.printStackTrace();
            //发送短信失败
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        //将发送的验证码保存到redis服务器中并设置清理时间
        jedisPool.getResource().setex(phoneNumber + RedisMessageConstant.SENDTYPE_ORDER, 300, DateUtils.parseDate2String(orderDate));
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    //预约成功之后,修改用户预约状态
    @RequestMapping("/updateOrderStatus")
    @PreAuthorize("hasAuthority('UPDATE_ORDERSETTING')")//修改预约状态数据权限
    public Result updateOrderStatus(Integer id) {
        try {
            //根据Order订单ID 修改预约状态
            orderService.updateOrderStatus(id);
            return new Result(true, MessageConstant.UPDATE_ORDERSETTINGLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ORDERSETTINGLIST_FAIL);
        }
    }

    //取消预约信息,并发送短信通知
    @RequestMapping("/deleteList/{id}")
    @PreAuthorize("hasAuthority('DELETE_ORDERSETTING')")//取消(删除)预约列表数据权限
    public Result deleteList(@PathVariable Integer id) {
        try {
            //调试
            //System.out.println("deleteList中接受的参数Id = " + id);
            //根据ID查询member会员表
            Member member = memberService.findByOrderId(id);
            //获取用户手机号
            String phoneNumber = member.getPhoneNumber();
            //给用户手机号发送,已取消预约的短信
            try {
                //使用阿里云短信服务工具类发送取消预约短信
                SMSUtils.sendShortMessage(SMSUtils.ORDER_CANCAL, phoneNumber, "");
                //调用删除方法
                orderSettingListService.deleteList(id);
            } catch (Exception e) {
                e.printStackTrace();
                //发送短信失败
                return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
            }
            return new Result(true, MessageConstant.DELETE_ORDERSETTINGLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDERSETTINGLIST_FAIL);
        }
    }

    //批量删除订单信息
    @RequestMapping("/deleteBatchesIds/{ids}")
    @PreAuthorize("hasAuthority('DELETE_ORDERSETTING')")//批量删除预约列表数据权限
    public Result deleteBatchesIds(@PathVariable Integer[] ids) {
        try {
            //遍历,获取到选中的每一个ID
            for (Integer id : ids) {
                //根据订单信息获取对应的会员信息
                Member member = memberService.findByOrderId(id);
                //获取到会员信息的手机号
                String phoneNumber = member.getPhoneNumber();
                //给用户手机号发送,已取消预约的短信
                try {
                    //使用阿里云短信服务工具类发送取消预约短信
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_CANCAL, phoneNumber, "");
                } catch (Exception e) {
                    e.printStackTrace();
                    //发送短信失败
                    return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
                }
                //根据ID删除每一个订单信息
                orderSettingListService.deleteList(id);
            }
            //删除成功
            return new Result(true, MessageConstant.DELETE_ORDERSETTINGLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_ORDERSETTINGLIST_FAIL);
        }
    }
}
